﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class UpdateDocumentServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateDocumentChecklistRequest, ResponseRecord>
    {
        private UserRecord Dao;
        private ChecklistDocumentRecord record;
        public UpdateDocumentServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateDocumentChecklistRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
            else
            {
                record = request.Record;
            }
            if (request.DaoChange == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.DaoChange;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            try
            {
                bool isUpdate = true;
                if (int.Parse(record.Id) != 0)
                {
                    CheckListDocument checkDoc = Providers.ProductHandbooks.RetrieveChecklistDocumentByDocId(int.Parse(record.Id));
                    if (checkDoc != null)
                    {
                        CheckListDocument updateRecord = new CheckListDocument
                        {
                            Document = record.Document,
                            ChecklistDocId = int.Parse(record.Id),
                            Note = record.Note,
                            Copy = record.Copy,
                            Original = record.Original,
                            Duplicate = record.Duplicate,
                            PictureDocument = record.PictureDoc,
                            DocumentGroup = record.DocumentGroup,
                            DocumentGroupCategory = record.DocumentGroupCategory
                        };
                        isUpdate = Providers.ProductHandbooks.UpdateDocumentChecklist(updateRecord);
                    }
                }
                else
                {
                    CheckListDocument updateRecord = new CheckListDocument
                    {
                        Document = record.Document,
                        ChecklistDocId = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds,
                        Note = record.Note,
                        Copy = record.Copy,
                        Original = record.Original,
                        Duplicate = record.Duplicate,
                        PictureDocument = record.PictureDoc,
                        DocumentGroup = record.DocumentGroup,
                        DocumentGroupCategory = record.DocumentGroupCategory,
                        SubProductId = record.SubProductId,
                        ProductId = record.ProductId
                    };
                    isUpdate = Providers.ProductHandbooks.SaveDocumentChecklist(updateRecord);
                }

                return isUpdate;
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
        }
    }
}
